package com.china08.yxyapi.api.manage.tec.teaching.lesson.cloud;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLessonItem;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLessonItemQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemQuesRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemRepository;
import com.china08.yxyapi.util.StringTrimUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class TecCloudLessonItemListController extends YxyApiBaseController {

    @Autowired
    YxyLessonsCloudLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @Autowired
    YxyLessonsCloudLessonItemQuesRepository itemQuesRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lesson/cloud/item/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;

        List<YxyLessonsCloudLessonItem> list = itemRepository.findByLessonIdAndStatusNotOrderByOrderNumAsc(lessonId, -1);
        List<RespModel> returnList = new ArrayList<RespModel>();
        for (YxyLessonsCloudLessonItem item : list) {
            RespModel respModel = new RespModel();

            String videoId = item.getVideoId();
            String itemId = item.getId();
            respModel.itemId = itemId;
            respModel.itemName = item.getTitle();
            respModel.intro = item.getIntro();
            respModel.videoId = videoId;
            respModel.status = item.getStatus();
            respModel.rejectReason = item.getRejectReason();
            respModel.orderNum = item.getOrderNum();

            // 获取习题
            List<YxyLessonsCloudLessonItemQues> itemQues = itemQuesRepository.findByLessonItemId(itemId);
            respModel.hasItemQues = itemQues.size() > 0;
            List<String> quesIdList = new ArrayList<String>();
            for (YxyLessonsCloudLessonItemQues itemQue : itemQues) {
                quesIdList.add(itemQue.getId());
            }
            respModel.quesIdList = quesIdList;
            YxyLessonsVideo video = videoRepository.findById(videoId);
            if (video != null) {
                Integer duration = video.getDuration();
                respModel.duration = StringTrimUtil.disDuration(duration);
            }
            returnList.add(respModel);
        }
        return returnList;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;
    }

    public static class RespModel extends YxyApiRespModel {
        public String itemId;
        public String itemName;
        public String intro;
        public String videoId;
        public Integer status;
        public String duration;
        public String rejectType;
        public String rejectReason;
        public Integer orderNum;
        public boolean hasItemQues = false;
        public List<String> quesIdList;
    }
}
